package com.huike.controller;

import com.huike.annotation.Log;
import com.huike.core.controller.BaseController;
import com.huike.core.domain.AjaxResult;
import com.huike.core.domain.R;
import com.huike.core.domain.entity.SysDictData;
import com.huike.core.domain.entity.SysDictType;
import com.huike.core.page.TableDataInfo;
import com.huike.enums.BusinessType;
import com.huike.service.DictService;
import com.huike.system.service.ISysDictTypeService;
import com.huike.utils.SecurityUtils;
import com.huike.utils.poi.ExcelUtil;
import com.huike.vo.DictVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/dict/data")
@Slf4j
public class DictController  extends BaseController {
    @Autowired
    private DictService dictService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 字典查询
     * @param dictType
     * @return
     */
    @GetMapping("/type/{dictType}")
    public R getDict(@PathVariable String dictType ) {
        log.info("字典查询，{}",dictType);
        List<DictVo> list=dictService.getDict(dictType);
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @GetMapping("/export")
    public AjaxResult export(SysDictData dictData)
    {
        List<SysDictData> list = dictService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictService.selectDictDataById(dictCode));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        return toAjax(dictService.deleteDictDataByIds(dictCodes));
    }

}
